define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/store/index' + location.search,
                    add_url: 'user/store/add',
                    edit_url: 'user/store/edit',
                    //del_url: 'user/store/del',
                    multi_url: 'user/store/multi',
                    import_url: 'user/store/import',
                    dragsort_url: '',
                    table: 'store',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 5,
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: true,//快速搜索
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.mobile', title: __('User_id'),operate: 'LIKE'},
                        {field: 'shop_name', title: __('Shop_name'), operate: 'LIKE'},
                        {field: 'business_hours', title: __('Business_hours'), operate: 'LIKE'},
                        /*{field: 'store_location', title: __('Store_location'), operate: 'LIKE'},
                        {field: 'lng', title: __('Lng'), operate:'BETWEEN'},
                        {field: 'lat', title: __('Lat'), operate:'BETWEEN'},*/
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'owner_name', title: __('Owner_name'), operate: 'LIKE'},
                        {field: 'owner_mobile', title: __('Owner_mobile'), operate: 'LIKE'},
                        {field: 'telephone', title: __('Telephone'), operate: 'LIKE'},
                        {field: 'vehicle_type', title: __('Vehicle_type'), searchList: {"10":__('Vehicle_type 10'),"20":__('Vehicle_type 20')}, formatter: Table.api.formatter.normal},
                        {field: 'category_ids', title: __('Category_ids'), operate: 'in',  visible: false,searchList: Config.categoryList},
                        {field: 'category_texts', title: __('Category_ids'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'shop_intro', title: __('Shop_intro'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'logo_image', title: __('Logo_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shop_door_image', title: __('Shop_door_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'prove_images', title: __('Prove_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'pay_way', title: __('Pay_way'), searchList: {"10":__('Pay_way 10'),"20":__('Pay_way 20'),"30":__('Pay_way 30')}, formatter: Table.api.formatter.normal},
                        {field: 'reason', title: __('Reason'), operate: 'LIKE'},
                        {field: 'verifytime', title: __('Verifytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'total_sales', title: __('Total_sales')},
                        {field: 'sales', title: __('Sales')},
                        {field: 'false_sales', title: __('False_sales')},
                        {field: 'likes', title: __('Likes')},
                        {field: 'collect', title: __('Collect')},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'transaction_id', title: __('Transaction_id'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'refundtime', title: __('Refundtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pay_status', title: __('Pay_status'), searchList: {"10":__('Pay_status 10'),"20":__('Pay_status 20')}, formatter: Table.api.formatter.label},
                        {field: 'refund_status', title: __('Refund_status'), searchList: {"10":__('Refund_status 10'),"20":__('Refund_status 20')}, formatter: Table.api.formatter.label},
                        {field: 'state', title: __('State'), searchList: {"10":__('State 10'),"20":__('State 20'),"30":__('State 30')}, formatter: Table.api.formatter.normal},
                        {field: 'recommend_switch', title: __('Recommend_switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'check',
                                    text: '通过审核',
                                    classname: 'btn btn-xs btn-success btn-click',
                                    icon: 'fa fa-eye',
                                    disable: function(row){//判断按钮是否禁用
                                        if(row.state != '10'){
                                            return true;
                                        }
                                        return false;
                                    },
                                    click: function (data,row) {
                                        layer.confirm('审核通过？', {
                                            btn: ['确定',  '取消'] //可以无限个按钮
                                            ,btn2: function(index, layero){
                                                layer.close();
                                            }
                                        }, function(index, layero){
                                            //按钮1
                                            $.post('user/store/pass/ids/'+row.id,{'row[state]':'20'},function(ret){
                                                layer.close(index);
                                                if(ret.code == 1){
                                                    Toastr.success('操作成功！');
                                                    //layer.msg('操作成功！');
                                                    $("#table").bootstrapTable('refresh',{});
                                                }else{
                                                    Toastr.error(ret.msg);
                                                }
                                            },'json');
                                        });
                                    }
                                },
                                {
                                    name: 'refuse',
                                    text: '审核失败',
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: 'fa fa-eye',
                                    disable: function(row){//判断按钮是否禁用
                                        if(row.state != '10'){
                                            return true;
                                        }
                                        return false;
                                    },
                                    click: function (data,row) {
                                        layer.prompt({
                                            title: '审核失败原因',
                                        }, function(value, index, elem){
                                            $.post('user/store/reject/ids/'+row.id,{'row[state]':'30','row[reason]':value},function(ret){
                                                layer.close(index);
                                                if(ret.code == 1){
                                                    Toastr.success('操作成功！');
                                                    $("#table").bootstrapTable('refresh',{});
                                                }else{
                                                    Toastr.error(ret.msg);
                                                }
                                            },'json');
                                        });
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'user/store/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'user/store/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'user/store/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
